/*
 * @author zzr
 * @date: 2025/03/20  18:42
 * @description:双向不带头非循环链表
 */
public class MyLinkedList {
    static class ListNode {
        private int val;
        private ListNode next;
        private ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    /**
     * 返回链表长度
     *
     * @return
     */
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    /**
     * 打印链表
     */
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 是否包含值为key的节点
     *
     * @param key
     * @return
     */
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (key == cur.val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /**
     * 头插法
     *
     * @param val
     */
    public void addFirst(int val) {

        ListNode node = new ListNode(val);
        if (null == head) {
            head = node;
            last = node;
        } else {
            head.prev = node;
            node.next = head;
            head = node;
        }
    }

    /**
     * 尾插法
     *
     * @param val
     */
    public void addLast(int val) {
        ListNode node = new ListNode(val);
        if (null == last) {
            head = node;
            last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    /**
     * 在指定下标index位置添加节点
     * @param index
     * @param val
     */
    public void addIndex(int index, int val) {

        checkIndex(index);

        if(index == 0) {
            addFirst(val);
            return;
        }
        if(index == size()) {
            addLast(val);
            return;
        }
        ListNode cur = findIndex(index);
        ListNode node = new ListNode(val);

        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = cur;
            node.prev = cur.prev;
            cur.prev.next = node;
            cur.prev = node;
        }

    }

    /**
     * 检查index合法性
     * @param index
     */
    private void checkIndex(int index) {
        if(index < 0 || index > size()) {
            throw new IndexOutOfBoundException("index不合法");
        }
    }

    /**
     * 找到下标为index的节点
     * @param index
     * @return
     */
    private ListNode findIndex(int index) {
        ListNode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    /**
     * 删除链表中值为val的节点
     * @param val
     */
    public void remove(int val) {
        ListNode cur = head;

        while(cur != null) {
            if(cur.val == val) {
                //找到了val相同的情况
                if(cur == head) {
                    //如果是头节点

                    //如果链表中只有一个头结点
                    //则cur.next.prev会空指针
                    if(head.next == null) {
                        head = null;
                        return;
                    }
                    cur.next.prev = null;
                    head = cur.next;
                } else {
                    //如果是中间节点
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    } else {
                        //尾巴节点
                        cur.prev.next = null;
                        last = cur.prev;
                    }
                }
                return;
                // 没删除节点 cur向后遍历继续找节点
            } else {
                cur = cur.next;
            }
        }
    }

    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    if(head.next == null) {
                        head = null;
                        return;
                    }
                    cur.next.prev = null;
                    head = head.next;
                } else {
                    if(cur.next != null) {
                        cur.next.prev = cur.prev;
                        cur.next.next = cur.next;
                    } else {
                        cur.prev.next = null;
                        last = last.prev;
                    }
                }
                cur = cur.next;
            } else {
                cur = cur.next;
            }
        }
    }

    public void clead() {
        head = null;
        last = null;
    }
}
